<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.random.Generator.multivariate_normal &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../../about.html" >
    <link rel="index" title="Index" href="../../../genindex.html" >
    <link rel="search" title="Search" href="../../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../../index.html" >
    <link rel="up" title="Random Generator" href="../generator.html" >
    <link rel="next" title="numpy.random.Generator.negative_binomial" href="numpy.random.Generator.negative_binomial.html" >
    <link rel="prev" title="numpy.random.Generator.multivariate_hypergeometric" href="numpy.random.Generator.multivariate_hypergeometric.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../../index.html">
      <img border=0 alt="NumPy" src="../../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../../routines.html" >Routines</a></li>
          <li class="active"><a href="../index.html" >Random sampling (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.random</span></code>)</a></li>
          <li class="active"><a href="../generator.html" accesskey="U">Random Generator</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.random.Generator.negative_binomial.html" title="numpy.random.Generator.negative_binomial"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.random.Generator.multivariate_hypergeometric.html" title="numpy.random.Generator.multivariate_hypergeometric"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.random.Generator.multivariate_hypergeometric.html"
                        title="previous chapter">numpy.random.Generator.multivariate_hypergeometric</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.random.Generator.negative_binomial.html"
                        title="next chapter">numpy.random.Generator.negative_binomial</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-random-generator-multivariate-normal">
<h1>numpy.random.Generator.multivariate_normal<a class="headerlink" href="#numpy-random-generator-multivariate-normal" title="Permalink to this headline">¶</a></h1>
<p>method</p>
<dl class="method">
<dt id="numpy.random.Generator.multivariate_normal">
<code class="sig-prename descclassname">Generator.</code><code class="sig-name descname">multivariate_normal</code><span class="sig-paren">(</span><em class="sig-param">mean</em>, <em class="sig-param">cov</em>, <em class="sig-param">size=None</em>, <em class="sig-param">check_valid='warn'</em>, <em class="sig-param">tol=1e-8</em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.random.Generator.multivariate_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Draw random samples from a multivariate normal distribution.</p>
<p>The multivariate normal, multinormal or Gaussian distribution is a
generalization of the one-dimensional normal distribution to higher
dimensions.  Such a distribution is specified by its mean and
covariance matrix.  These parameters are analogous to the mean
(average or “center”) and variance (standard deviation, or “width,”
squared) of the one-dimensional normal distribution.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>mean</strong><span class="classifier">1-D array_like, of length N</span></dt><dd><p>Mean of the N-dimensional distribution.</p>
</dd>
<dt><strong>cov</strong><span class="classifier">2-D array_like, of shape (N, N)</span></dt><dd><p>Covariance matrix of the distribution. It must be symmetric and
positive-semidefinite for proper sampling.</p>
</dd>
<dt><strong>size</strong><span class="classifier">int or tuple of ints, optional</span></dt><dd><p>Given a shape of, for example, <code class="docutils literal notranslate"><span class="pre">(m,n,k)</span></code>, <code class="docutils literal notranslate"><span class="pre">m*n*k</span></code> samples are
generated, and packed in an <em class="xref py py-obj">m</em>-by-<em class="xref py py-obj">n</em>-by-<em class="xref py py-obj">k</em> arrangement.  Because
each sample is <em class="xref py py-obj">N</em>-dimensional, the output shape is <code class="docutils literal notranslate"><span class="pre">(m,n,k,N)</span></code>.
If no shape is specified, a single (<em class="xref py py-obj">N</em>-D) sample is returned.</p>
</dd>
<dt><strong>check_valid</strong><span class="classifier">{ ‘warn’, ‘raise’, ‘ignore’ }, optional</span></dt><dd><p>Behavior when the covariance matrix is not positive semidefinite.</p>
</dd>
<dt><strong>tol</strong><span class="classifier">float, optional</span></dt><dd><p>Tolerance when checking the singular values in covariance matrix.
cov is cast to double before the check.</p>
</dd>
<dt><strong>method</strong><span class="classifier">{ ‘svd’, ‘eigh’, ‘cholesky’}, optional</span></dt><dd><p>The cov input is used to compute a factor matrix A such that
<code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre">&#64;</span> <span class="pre">A.T</span> <span class="pre">=</span> <span class="pre">cov</span></code>. This argument is used to select the method
used to compute the factor matrix A. The default method ‘svd’ is
the slowest, while ‘cholesky’ is the fastest but less robust than
the slowest method. The method <em class="xref py py-obj">eigh</em> uses eigen decomposition to
compute A and is faster than svd but slower than cholesky.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.18.0.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl>
<dt><strong>out</strong><span class="classifier">ndarray</span></dt><dd><p>The drawn samples, of shape <em>size</em>, if that was provided.  If not,
the shape is <code class="docutils literal notranslate"><span class="pre">(N,)</span></code>.</p>
<p>In other words, each entry <code class="docutils literal notranslate"><span class="pre">out[i,j,...,:]</span></code> is an N-dimensional
value drawn from the distribution.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>The mean is a coordinate in N-dimensional space, which represents the
location where samples are most likely to be generated.  This is
analogous to the peak of the bell curve for the one-dimensional or
univariate normal distribution.</p>
<p>Covariance indicates the level to which two variables vary together.
From the multivariate normal distribution, we draw N-dimensional
samples, <img class="math" src="../../../_images/math/7e7cbe004b0d97071d449df48c47de3cf31a5609.svg" alt="X = [x_1, x_2, ... x_N]"/>.  The covariance matrix
element <img class="math" src="../../../_images/math/6b70bcae52a568a124c4c8024147cabbfb28ce2f.svg" alt="C_{ij}"/> is the covariance of <img class="math" src="../../../_images/math/c67734af70861b2bd4dedf5c41c9aad231466f84.svg" alt="x_i"/> and <img class="math" src="../../../_images/math/ab9afdaf786ce53318d75d81f050af8560822fcd.svg" alt="x_j"/>.
The element <img class="math" src="../../../_images/math/e0230477f2ec2f4d92596220cec9555ca8d99c84.svg" alt="C_{ii}"/> is the variance of <img class="math" src="../../../_images/math/c67734af70861b2bd4dedf5c41c9aad231466f84.svg" alt="x_i"/> (i.e. its
“spread”).</p>
<p>Instead of specifying the full covariance matrix, popular
approximations include:</p>
<blockquote>
<div><ul class="simple">
<li><p>Spherical covariance (<em class="xref py py-obj">cov</em> is a multiple of the identity matrix)</p></li>
<li><p>Diagonal covariance (<em class="xref py py-obj">cov</em> has non-negative elements, and only on
the diagonal)</p></li>
</ul>
</div></blockquote>
<p>This geometrical property can be seen in two dimensions by plotting
generated data-points:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mean</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cov</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">]]</span>  <span class="c1"># diagonal covariance</span>
</pre></div>
</div>
<p>Diagonal covariance means that points are oriented along x or y-axis:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">default_rng</span><span class="p">()</span><span class="o">.</span><span class="n">multivariate_normal</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="n">cov</span><span class="p">,</span> <span class="mi">5000</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;equal&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that the covariance matrix must be positive semidefinite (a.k.a.
nonnegative-definite). Otherwise, the behavior of this method is
undefined and backwards compatibility is not guaranteed.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r73135c1078a9-1"><span class="brackets">1</span></dt>
<dd><p>Papoulis, A., “Probability, Random Variables, and Stochastic
Processes,” 3rd ed., New York: McGraw-Hill, 1991.</p>
</dd>
<dt class="label" id="r73135c1078a9-2"><span class="brackets">2</span></dt>
<dd><p>Duda, R. O., Hart, P. E., and Stork, D. G., “Pattern
Classification,” 2nd ed., New York: Wiley, 2001.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mean</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cov</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">default_rng</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">multivariate_normal</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="n">cov</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 3, 2)</span>
</pre></div>
</div>
<p>We can use a different method other than the default to factorize cov:
&gt;&gt;&gt; y = rng.multivariate_normal(mean, cov, (3, 3), method=’cholesky’)
&gt;&gt;&gt; y.shape
(3, 3, 2)</p>
<p>The following is probably true, given that 0.6 is roughly twice the
standard deviation:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">((</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">-</span> <span class="n">mean</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">0.6</span><span class="p">)</span>
<span class="go">[True, True] # random</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>